package com.wc.alorithm_blue_bridge._二分.Q3611;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Arrays;
import java.util.Comparator;

/**
 * @Author congege
 * @Description
 * @Date Created in 2023/11/24-14:18
 */
public class Main2 {
    public static void main(String[] args) {
        Input sc = new Input();
        int n = sc.nextInt();
        int m = sc.nextInt();
        Integer[] powers = new Integer[n];
        for (int i = 0; i < n; i++) {
            powers[i] = sc.nextInt();
        }
        Comparator<Integer> comparator = (o1, o2) -> o2 - o1;
        Arrays.sort(powers);
        Integer[][] tasks = new Integer[1000][1000];
        for (int i = 0; i < 1000; i++) {
            Arrays.fill(tasks[i], 0);
        }

        int maxK = 0;
        for (int i = 0; i < m; i++) {
            int k = sc.nextInt();
            maxK = Math.max(maxK, k);
            for (int j = 0; j < k; j++) {
                tasks[i][j] = sc.nextInt();
            }
            Arrays.sort(tasks[i], comparator);
        }
        // 将tasks 转置一下
        for (int i = 0; i < maxK; i++) {
            for (int j = i + 1; j < maxK; j++) {
                int tmp = tasks[i][j];
                tasks[i][j] = tasks[j][i];
                tasks[j][i] = tmp;
            }
        }

        Integer[] maxTask = new Integer[maxK];

        // 将每一行倒置排序提取
        for (int i = 0; i < maxK; i++) {
            Arrays.sort(tasks[i], comparator);
            maxTask[i] = tasks[i][0];
        }
        // 这个排序就不许要从大到小了
        Arrays.sort(maxTask);
        // 存储刚刚好大于的位置
        int maxIndex = 0;
        // 存储答案
        long ans = 0;
        int i = 0;
        for (; i < maxK; i++) {
            maxIndex = search(maxTask[i], powers, maxIndex, n - 1);
            if (maxIndex >= n) {
                System.out.println(-1);
                return;
            }
            ans += powers[maxIndex];
            maxIndex++;
        }
        if (i != maxK) {
            System.out.println(-1);
            return;
        }
        System.out.println(ans);
    }

    // 寻找到数组中刚好大于val的位置
    static public int search(int val, Integer[] array, int left, int right) {
        int mid;
        while (left <= right) {
            mid = left + (right - left) / 2;
            if (array[mid] > val) {
                right = mid - 1;
            } else if (array[mid] < val) {
                left = mid + 1;
            } else {
                return mid;
            }
        }

        return left;
    }

    static class Input {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        public int nextInt() {
            try {
                in.nextToken();
            } catch (Exception e) {
                return -1;
            }
            return (int) in.nval;
        }
    }
}
